#include "maincar.h"
#include "ui_maincar.h"


mainCar::mainCar(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::mainCar)
{
    ui->setupUi(this);

    hardInterface = new HardInterface(ui->spinBox->value(),
                                      ui->spinBox_2->value());
    netCtl = new NetControl();
    carCtl = new CarControl(hardInterface, netCtl, ui);

    connect(carCtl,SIGNAL(updateGui()),
            this,SLOT(updateGui()));
    connect(carCtl,SIGNAL(sendDataSig(QByteArray)),
            hardInterface,SLOT(sendDataToSim(QByteArray)));
    connect(hardInterface->commCtl,SIGNAL(broadcastReadyRead(QHostAddress, quint16, QByteArray)),
            netCtl,SLOT(broadcastReceived(QHostAddress,quint16,QByteArray)));
    connect(ui->spinBox,SIGNAL(valueChanged(int)),
            carCtl, SLOT(tryChangeSpeed(int)));
    connect(ui->spinBox_2,SIGNAL(valueChanged(int)),
            carCtl,SLOT(tryChangeSpur(int)));
}

mainCar::~mainCar()
{
    delete ui;
    delete hardInterface;
    delete netCtl;
    delete carCtl;
}

void mainCar::updateGui()
{
    QString tempS("S: ");
    QString tempW("  W: ");
    //update dieses auto
    QString carid;
    carid.setNum(carCtl->getCarId(), 10);
    QString id("ID: ");
    id.append(carid);
    ui->label_10->setText(id);

    carid.setNum(carCtl->getCarSpeed(), 10);
    ui->textBrowser_8->setText(carid);

    carid.setNum(carCtl->getCarSpur(), 10);
    QString spur("S: ");
    spur.append(carid);
    ui->textBrowser_2->setText(spur);

    carid.setNum(carCtl->getCarWeite(), 10);
    spur.clear();
    spur.append("W: ");
    spur.append(carid);
    ui->textBrowser_7->setText(spur);

    //update auto davor
    carid.setNum(carCtl->netCtl->dovorAuto.getId(), 10);
    ui->label_14->setText(carid);

    carid.setNum(carCtl->netCtl->dovorAuto.getSpeed(), 10);
    ui->textBrowser_4->setText(carid);

    carid.setNum(carCtl->netCtl->dovorAuto.getSpur(), 10);
    tempS.append(carid);
    tempS.append(tempW);
    carid.setNum(carCtl->netCtl->dovorAuto.getWeite(), 10);
    tempS.append(carid);
    ui->textBrowser_5->setText(tempS);

    //update auto danach
    carid.setNum(carCtl->netCtl->danachAuto.getId(), 10);
    ui->label_15->setText(carid);

    carid.setNum(carCtl->netCtl->danachAuto.getSpeed(), 10);
    ui->textBrowser_3->setText(carid);

    tempS.clear();
    tempS.append("S: ");
    carid.setNum(carCtl->netCtl->danachAuto.getSpur(), 10);
    tempS.append(carid);
    tempS.append(tempW);
    carid.setNum(carCtl->netCtl->danachAuto.getWeite(), 10);
    tempS.append(carid);
    ui->textBrowser_6->setText(tempS);
}

